package org.high5.paintmyworld;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import org.high5.model.CanvasCoordinates;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;



public class MainActivity extends MapActivity {

	public final String TAG = "PaintMyWorld-MainActivity";
	public static Context context;
	private Timer myTimer;
	private PrettyLine line;
	private MapView mapView;


	@Override
	public void onCreate(Bundle savedInstanceState) {

		// standard onCreate body
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this.getApplicationContext();
		
		// implementing maps
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(false);
		mapView.displayZoomControls(false);
		mapView.setSatellite(true);
		
		// config action bar
		ActionBar bar = this.getActionBar();
		bar.setLogo(R.drawable.logo);
		
		//addding the map's overlay
		ImageView iv = (ImageView) findViewById(R.id.imageView);
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		
		int width = size.x;
		int height = size.y-getActionBar().getHeight();
		
        line = new PrettyLine(width, height, iv);
        
        CanvasCoordinates[] cc = new CanvasCoordinates[9];

        cc[0]=new CanvasCoordinates(400,60,100);
        cc[1]=new CanvasCoordinates(450,500,200);
        cc[2]=new CanvasCoordinates(100,660,500);
        cc[3]=new CanvasCoordinates(6000,500,500);
        cc[4]=new CanvasCoordinates(550,20,100);
        cc[5]=new CanvasCoordinates(700,250,10000);
        cc[6]=new CanvasCoordinates(450,900,200);
        cc[7]=new CanvasCoordinates(50,0,10000);
        cc[8]=new CanvasCoordinates(800,1100,200);
        
        try {
			line.redraw(cc);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
        
        myTimer = new Timer();
		myTimer.schedule(new TimerTask() {			
			@Override
			public void run() {
				TimerMethod();
			}
			
		}, 0, 80);
		
        

	}
	
	// method to allow other team member to access context of the MainActivity
	public static Context getMainActivityContext() {
		return context;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// one of the item in the actionbar has been selected, now we determine which one
		switch (item.getItemId()) {
		case R.id.menu_share2:
			Log.i(TAG,"User has pressed share icon");
			Intent shareIntent = new Intent(Intent.ACTION_SEND);
			shareIntent.setType("image/jpeg");
			Uri uri2 = Uri.fromFile(getScreen());
			shareIntent.putExtra(Intent.EXTRA_STREAM, uri2);
			startActivity(Intent.createChooser(shareIntent, "Share Map"));
			return true;
		case R.id.menu_settings:
			Log.i(TAG,"User has pressed settings icon");			
			Intent start = new Intent(this, PrefsActivity.class);
			startActivity(start);
		default:
			return super.onOptionsItemSelected(item);
		}

	}


	// method to get a screenshot of mapview
	private File getScreen() {
		Log.i(TAG,"Generating Screenshot ready for sharing");
		View content = findViewById(R.id.frame_layout);
		content.setDrawingCacheEnabled(true);
		content.invalidate();
		Bitmap bitmap = content.getDrawingCache();
		File file = new File(
				getExternalFilesDir(Environment.DIRECTORY_PICTURES), "imageofmap.png");
		try {
			file.createNewFile();
			FileOutputStream ostream = new FileOutputStream(file);
			bitmap.compress(CompressFormat.PNG, 100, ostream);
			ostream.close();

		} catch (IOException e) {
			Log.e(TAG, "IOException");
		}

		return file;
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		myTimer.cancel();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void TimerMethod()
	{
		//This method is called directly by the timer
		//and runs in the same thread as the timer.

		//We call the method that will work with the UI
		//through the runOnUiThread method.
		this.runOnUiThread(Timer_Tick);
	}


	private Runnable Timer_Tick = new Runnable() {
		public void run() {
		
		//This method runs in the same thread as the UI.    	       
		
		//Do something to the UI thread here
			line.draw();
		}
	};
	
	
	
	

}
